﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SMG.Domain.Repository;
using SMG.Domain.Repository.Program;
using SMG.ServiceContract.Program;
using SMG.Application.DTO.Program;

using SMG.Application.Extension;
using Infrastructure.Core.Common;
using SMG.Domain.Specifications;
using SMG.Domain.Specifications.Program;
using SMG.Domain.Model.Program;

namespace SMG.Application.Program
{
    /// <summary>
    /// 节目单服务实现
    /// </summary>
    public class ProgramBillServiceImpl : ApplicationService, IProgramBillService
    {
        IProgramBillRepository billRepository;
        IBillVersionRecordRepository billVersionRepository;

        public ProgramBillServiceImpl(IRepositoryContext context, 
                                      IProgramBillRepository billRepository,
                                      IBillVersionRecordRepository billVersionRepository)
            : base(context)
        {
            this.billRepository = billRepository;
            this.billVersionRepository = billVersionRepository;

            this.billRepository.SetContext(context);
            this.billVersionRepository.SetContext(context);
        }

        /// <summary>
        /// 添加节目单
        /// </summary>
        /// <param name="billDto"></param>
        /// <returns></returns>
        public bool InsertProgramBill(ProgramBillDto billDto)
        {
            if (billDto == null)
                throw new ArgumentNullException("节目单对象billDto为null");

            billRepository.Create(billDto.ConvertToBill());
            return Context.Commit();
        }

        /// <summary>
        /// 批量添加节目单
        /// </summary>
        /// <param name="billDto"></param>
        /// <returns></returns>
        public bool InsertBatchProgramBill(List<ProgramBillDto> billDtos)
        {
            if (billDtos == null || !billDtos.Any())
                throw new ArgumentNullException("节目单对象列表billDtos为null");

            billRepository.InsertBatch(billDtos.ConvertToBill().ToList());
            return Context.Commit();
        }

        /// <summary>
        /// 批量添加节目单
        /// </summary>
        /// <param name="billDto"></param>
        /// <returns></returns>
        public bool InsertBatchProgramBill(BillVersionRecordDto billVersionDto, List<ProgramBillDto> billDtos)
        {
            if (billVersionDto == null)
                throw new ArgumentNullException("节目单版本对象billVersionDto为null");

            billVersionRepository.Create(billVersionDto.ConvertToBillVersion());
            if (billDtos != null && billDtos.Any()) {
                billDtos.ForEach(dto =>
                {
                    dto.VersionId = billVersionDto.ID;
                });
                billRepository.InsertBatch(billDtos.ConvertToBill().ToList());
            }
                
            return Context.Commit();
        }

        #region Query

        /// <summary>
        /// 查询节目单版本列表
        /// </summary>
        /// <param name="channelId"></param>
        /// <param name="playDate"></param>
        /// <returns></returns>
        public IEnumerable<BillVersionRecordDto> QueryBillVersion(String channelId, String playDate)
        {
            var billVersions = billVersionRepository.QueryBy(new AndSpecification<BillVersionRecord>(
                                                    new BillVersionChannelEqualSpecification(channelId),
                                                    new BillVersionDateEqualSpecification(playDate)));
            return billVersions.ToList().ConvertToBillVersionDto();
        }

        /// <summary>
        /// 获取某个版本下的节目单列表
        /// </summary>
        /// <param name="versionId"></param>
        /// <returns></returns>
        public IEnumerable<ProgramBillDto> QueryBillByVersion(long versionId)
        {
            var bills = billRepository.QueryBy(new BillVersionIdEqualSpecification(versionId));
            return bills.ToList().ConvertToProgramBillDto();
        }

        /// <summary>
        /// 分页获取某个版本下的节目单列表
        /// </summary>
        /// <param name="versionId"></param>
        /// <returns></returns>
        public PageResult<ProgramBillDto> QueryBillByVersion(long versionId, Int32 pageIndex, Int32 pageSize)
        {
            var result = new PageResult<ProgramBillDto>()
            {
                PageSize = pageSize,
                PageIndex = pageIndex,
                TotalRecords = 0,
            };
            var pageBills = billRepository.QueryByPage(new BillVersionIdEqualSpecification(versionId), b => b.ItemIndex, SortOrder.Ascending, pageSize, pageIndex);
            if (pageBills != null)
            {
                result.TotalRecords = pageBills.TotalRecords;
                result.Data = pageBills.Data.ConvertToProgramBillDto();
            }
            return result;
        }

        /// <summary>
        /// 分页查询节目单列表
        /// </summary>
        /// <param name="channelId"></param>
        /// <param name="playDate"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public PageResult<ProgramBillDto> QueryBillByPage(String channelId, String playDate, Int32 pageIndex, Int32 pageSize)
        {
            throw new NotImplementedException();
        }
        
        #endregion
        
    }
}
